﻿<?php

//折翼天使资源社区 提供 www.zheyitianshi.com
global $_W, $_GPC;
$mid = intval($_GPC['mid']);
$qid = intval($_GPC['qid']);
$openid = m('user')->getOpenid();
$member = m('member')->getMember($openid);
$uniacid = $_W['uniacid'];
// if (!empty($mid)) {
// 	header('location: ' . $this->createMobileUrl('shop'));
// 	die;
// }else{
// 	header('location: ' . $this->createMobileUrl('shop'));
// 	die;
// }
if (!empty($mid)) {
	if (!$this->model->isAgent($mid)){
		header('location: ' . $this->createMobileUrl('shop'));
		die;
	}
//	if ($mid != $member['id'] && $member['isagent'] == 1 && $member['status'] == 1) {
//		header('location: ' . $this->createPluginMobileUrl('commission/myshop', array('mid' => $member['id'])));
//		die;
//	}
} else {
	if ($member['isagent'] == 1 && $member['status'] == 1) {
		$mid = $member['id'];
	} else {
		header('location: ' . $this->createMobileUrl('shop'));
		die;
	}
}
$shop = set_medias($this->model->getShop($mid), array('img', 'logo'));
//图片路径修改2016-07-30
$commission_shop= pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $mid));
if($commission_shop['logo']){
   $shop['logo']="http://wx.lanxuevip.com/attachment/".$commission_shop['logo'];
}
if($commission_shop['img']){
   $shop['img']="http://wx.lanxuevip.com/attachment/".$commission_shop['img'];  
}else{
   $shop['img']="http://wx.lanxuevip.com/attachment/images/5/2016/11/Kzmu53JwCTsEwDuM23745IcrM1cu4i.jpg";
}
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($op == 'display') {
	if ($_W['isajax']) {
		/*if (empty($shop['selectgoods'])) {
			$goodscount = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_goods') . ' where uniacid=:uniacid and status=1 and deleted=0', array(':uniacid' => $_W['uniacid']));
		} else {
			$goodscount = count(explode(",", $shop['goodsids']));
		}*/
                if($shop['goodsids']){
                $goodscount = count(explode(",", $shop['goodsids']));
                }else{
                $goodscount=0;    
                }
		$advs = pdo_fetchall("select id,advname,link,thumb from " . tablename('ewei_shop_adv') . ' where uniacid=:uniacid and enabled=1 order by displayorder desc', array(':uniacid' => $uniacid));
		$advs = set_medias($advs, 'thumb');
		$ret = array('shop' => $shop, 'goodscount' => number_format($goodscount, 0), 'set' => m('common')->getSysset('shop'), 'advs' => $advs);
		$ret['isme'] = $mid == $member['id'];
		show_json(1, $ret);
	}
	$_W['shopshare'] = array('title' => $shop['name'], 'imgUrl' => $shop['logo'], 'desc' => $shop['desc'], 'link' => $this->createMobileUrl('shop'));
	if ($member['isagent'] == 1 && $member['status'] == 1) {
		$_W['shopshare']['link'] = $this->createPluginMobileUrl('commission/myshop', array('mid' => $member['id']));
		if (empty($this->set['become_reg']) && (empty($member['realname']) || empty($member['mobile']))) {
			$trigger = true;
		}
	} else {
		if (!empty($mid)) {
			$_W['shopshare']['link'] = $this->createPluginMobileUrl('commission/myshop', array('mid' => $_GPC['mid']));
		}
	}

        /*
        *  gwj
        *  修改：会员发展会员
        *  时间：2016年10月8日
        */
       if($mid != $member['id'] && !empty($qid)){
               if(!$member['agentid']){
                       //更新为下级
                       $parent=pdo_fetch('select * from'.tablename('ewei_shop_member')."where id='{$mid}' and uniacid='{$uniacid}'");
                       $set = $this->getSet();
		       $tm = $set['tm'];
                       $templateid = $tm['templateid'];
                       $message = $tm['commission_agent_new'];
                       $msg = array('keyword1' => array('value' => !empty($tm['commission_agent_newtitle']) ? $tm['commission_agent_newtitle'] : '新增下线通知', "color" => "#73a68d"), 'keyword2' => array('value' => $message, "color" => "#73a68d"));
                       if (!empty($templateid)) {
                                m('message')->sendTplNotice($parent['openid'], $templateid, $msg);
                        } else {
                                m('message')->sendCustomNotice($parent['openid'], $msg);
                        }
                        pdo_query('update ' . tablename('ewei_shop_member') . " set agentid='{$mid}' where id=:id and uniacid='{$uniacid}' ", array(":id" => $member['id']));
               }
       }
	$this->setHeader();
	include $this->template('myshop');
} else {
	if ($op == 'goods') {
		if ($_W['isajax']) {
			$args = array('page' => $_GPC['page'], 'pagesize' => 6, 'order' => 'displayorder desc,createtime desc', 'by' => '');
			if (!empty($shop['selectgoods'])) {
				$goodsids = explode(',', $shop['goodsids']);
				if (!empty($goodsids)) {
					$args['ids'] = trim($shop['goodsids']);
				}
			}
			$goods = m('goods')->getList($args);
                        if(empty($shop['goodsids'])){
                         $goods=array();   
                        }
			show_json(1, array('goods' => $goods, 'pagesize' => $args['pagesize']));
		}
	} else {
		if ($op == 'set') {
			if ($_W['isajax']) {
				if ($_W['ispost']) {
					$shopdata = is_array($_GPC['shopdata']) ? $_GPC['shopdata'] : array();
					$shopdata['uniacid'] = $_W['uniacid'];
					$shopdata['mid'] = $member['id'];
					if (empty($shop['id'])) {
						pdo_insert('ewei_shop_commission_shop', $shopdata);
					} else {
						pdo_update('ewei_shop_commission_shop', $shopdata, array('id' => $shop['id']));
					}
					show_json(1);
				}
				$shop = pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $member['id']));
				$shop = set_medias($shop, array('img', 'logo'));
                                //图片路径修改2016-07-30
                                $commission_shop= pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $mid));
                                if($commission_shop){
                                   $shop['logo']="http://wx.lanxuevip.com/attachment/".$commission_shop['logo'];
                                   $shop['img']="http://wx.lanxuevip.com/attachment/".$commission_shop['img'];
                                }
				$openselect = false;
				if ($this->set['select_goods'] == '1') {
					if (empty($member['agentselectgoods']) || $member['agentselectgoods'] == 2) {
						$openselect = true;
					}
				} else {
					if ($member['agentselectgoods'] == 2) {
						$openselect = true;
					}
				}
				$shop['openselect'] = $openselect;
				show_json(1, array('shop' => $shop));
			}
			include $this->template('myshop_set');
		} else {
			if ($op == 'select') {
				if ($_W['isajax']) {
					if ($member['agentselectgoods'] == 1) {
						show_json(-1, '您无权自选商品，请和运营商联系!');
					}
                                        if ($member['isShop'] != 1) {
						show_json(-1, '您无权自选商品，请和运营商联系!');
					}
					if (empty($this->set['select_goods'])) {
						if ($member['agentselectgoods'] != 2) {
							show_json(-1, '系统未开启自选商品!');
						}
					}
					$shop = pdo_fetch('select * from ' . tablename('ewei_shop_commission_shop') . ' where uniacid=:uniacid and mid=:mid limit 1', array(':uniacid' => $_W['uniacid'], ':mid' => $member['id']));
					if ($_W['ispost']) {
						$shopdata['selectgoods'] = intval($_GPC['selectgoods']);
						$shopdata['selectcategory'] = intval($_GPC['selectcategory']);
						$shopdata['uniacid'] = $_W['uniacid'];
						$shopdata['mid'] = $member['id'];
						if (is_array($_GPC['goodsids'])) {
							$shopdata['goodsids'] = implode(",", $_GPC['goodsids']);
						}
						if (!empty($shopdata['selectgoods']) && !is_array($_GPC['goodsids'])) {
							show_json(0, '请选择商品!');
						}
						if (empty($shop['id'])) {
							pdo_insert('ewei_shop_commission_shop', $shopdata);
						} else {
							pdo_update('ewei_shop_commission_shop', $shopdata, array('id' => $shop['id']));
						}
						show_json(1);
					}
					$goods = array();
					if (!empty($shop['selectgoods'])) {
						$goodsids = explode(',', $shop['goodsids']);
						if (!empty($goodsids)) {
							$goods = pdo_fetchall('select id,title,marketprice,thumb from ' . tablename('ewei_shop_goods') . ' where uniacid=:uniacid and id in ( ' . trim($shop['goodsids']) . ')', array(':uniacid' => $_W['uniacid']));
							$goods = set_medias($goods, 'thumb');
						}
					}
					show_json(1, array('shop' => $shop, 'goods' => $goods));
				}
				$set = m('common')->getSysset('shop');
				include $this->template('myshop_select');
                        }else{
                            if($op == 'center'){
                                $set = m('common')->getSysset(array('shop', 'trade'));
                                if ($_W['isajax']) {
                                        $member = m('member')->getInfo($openid);
                                        $member['credit1'] = number_format($member['credit1'], 0);
                                        $member['credit2'] = number_format($member['credit2'], 2);
                                        $level = array('levelname' => empty($set['shop']['levelname']) ? '普通会员' : $set['shop']['levelname']);
                                        if (!empty($member['level'])) {
                                                $level = m('member')->getLevel($openid);
                                        }
                                        $orderparams = array(':uniacid' => $_W['uniacid'], ':openid' => $openid);
                                        $order = array('status0' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and status=0  and uniacid=:uniacid limit 1', $orderparams), 'status1' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and status=1 and refundid=0 and uniacid=:uniacid limit 1', $orderparams), 'status2' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and status=2 and refundid=0 and uniacid=:uniacid limit 1', $orderparams), 'status4' => pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_order') . ' where openid=:openid and refundid<>0 and uniacid=:uniacid limit 1', $orderparams));
                                        if (mb_strlen($member['nickname'], 'utf-8') > 6) {
                                                $member['nickname'] = mb_substr($member['nickname'], 0, 6, 'utf-8');
                                        }
                                        $open_creditshop = false;
                                        $creditshop = p('creditshop');
                                        if ($creditshop) {
                                                $creditshop_set = $creditshop->getSet();
                                                if (!empty($creditshop_set['centeropen'])) {
                                                        $open_creditshop = true;
                                                }
                                        }
                                        show_json(1, array('member' => $member, 'order' => $order, 'level' => $level, 'open_creditshop' => $open_creditshop,'mid'=>$mid));
                                }
                                /*
                                 *  gmm
                                 *  修改：个人中心增加分销中心
                                 *  时间：2015年11月19日
                                 */
                                $member = m('member')->getMember($openid);
                                $gift_card=pdo_fetch('select * from '.tablename('mc_members').'where uniacid = :uniacid and uid=:uid',array(':uniacid' => $_W['uniacid'],':uid'=>$member['uid']));
                                $isagent = $member['isagent'] == 1 && $member['status'] == 1;
                                //gmm 2015-12-21 大伙伴审核人数小圆点
                                if($isagent){
                                        if($member['agentlevel'] == 1 || $member['agentlevel'] == 2){		//战略或大伙伴
                                                $confirm_num = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member') . " where uniacid = :uniacid and zhaomu = 1 and status = 0 and isagent = 1 and agentid = :agentid ", 
                                                                array(':uniacid' => $_W['uniacid'], ':agentid' => $member['id']));
                                        }else{
                                                $confirm_num = 0;
                                        }
                                }
                                include $this->template('center');    
                            }else{
                                            if($op == 'cart'){
                                            if ($_W['isajax']) {
                                                if ($op == 'cart') {
                                                    $condition = ' and f.uniacid= :uniacid and f.openid=:openid and f.deleted=0';
                                                    $params = array(':uniacid' => $uniacid, ':openid' => $openid);
                                                    $list = array();
                                                    $total = 0;
                                                    $totalprice = 0;
                                                    $sql = 'SELECT f.id,f.total,f.goodsid,g.total as stock, o.stock as optionstock, g.maxbuy,g.title,g.thumb,ifnull(o.marketprice, g.marketprice) as marketprice,g.productprice,o.title as optiontitle,f.optionid,o.specs FROM ' . tablename('ewei_shop_member_cart') . ' f ' . ' left join ' . tablename('ewei_shop_goods') . ' g on f.goodsid = g.id ' . ' left join ' . tablename('ewei_shop_goods_option') . ' o on f.optionid = o.id ' . ' where 1 ' . $condition . ' ORDER BY `id` DESC ';
                                                    $list = pdo_fetchall($sql, $params);
                                                    foreach ($list as &$r) {
                                                            if (!empty($r['optionid'])) {
                                                                    $r['stock'] = $r['optionstock'];
                                                            }
                                                            $totalprice += $r['marketprice'] * $r['total'];
                                                            $total += $r['total'];
                                                    }
                                                    unset($r);
                                                    $list = set_medias($list, 'thumb');
                                                    $totalprice = number_format($totalprice, 2);
                                                    show_json(1, array('total' => $total, 'list' => $list, 'totalprice' => $totalprice));
                                                } else {
                                                    if ($op == 'add' && $_W['ispost']) {
                                                            $id = intval($_GPC['id']);
                                                            $total = intval($_GPC['total']);
                                                            empty($total) && ($total = 1);
                                                            $optionid = intval($_GPC['optionid']);
                                                            $goods = pdo_fetch('select id,marketprice from ' . tablename('ewei_shop_goods') . ' where uniacid=:uniacid and id=:id limit 1', array(':uniacid' => $uniacid, ':id' => $id));
                                                            if (empty($goods)) {
                                                                    show_json(0, '商品未找到');
                                                            }
                                                            $data = pdo_fetch('select id from ' . tablename('ewei_shop_member_cart') . ' where openid=:openid and goodsid=:id and  optionid=:optionid and deleted=0 and  uniacid=:uniacid   limit 1', array(':uniacid' => $uniacid, ':openid' => $openid, ':optionid' => $optionid, ':id' => $id));
                                                            $cartcount = pdo_fetchcolumn('select count(id) from ' . tablename('ewei_shop_member_cart') . ' where openid=:openid and deleted=0 and uniacid=:uniacid limit 1', array(':uniacid' => $uniacid, ':openid' => $openid));
                                                            if (empty($data)) {
                                                                    $data = array('uniacid' => $uniacid, 'openid' => $openid, 'goodsid' => $id, 'optionid' => $optionid, 'marketprice' => $goods['marketprice'], 'total' => $total, 'createtime' => time());
                                                                    pdo_insert('ewei_shop_member_cart', $data);
                                                                    $cartcount += $total;
                                                                    show_json(1, array('message' => '添加成功', 'cartcount' => $cartcount));
                                                            }
                                                            show_json(1, array('message' => '已在购物车', 'cartcount' => $cartcount));
                                                    } else {
                                                            if ($op == 'selectoption' && $_W['ispost']) {
                                                                    $id = intval($_GPC['id']);
                                                                    $goodsid = intval($_GPC['goodsid']);
                                                                    $cartdata = pdo_fetch("SELECT id,optionid,total FROM " . tablename('ewei_shop_member_cart') . " WHERE id = :id and uniacid=:uniacid and openid=:openid limit 1", array(':id' => $id, ':uniacid' => $uniacid, ':openid' => $openid));
                                                                    $cartoption = pdo_fetch("select id,title,thumb,marketprice,productprice,costprice, stock,weight,specs from " . tablename('ewei_shop_goods_option') . " " . " where uniacid=:uniacid and goodsid=:goodsid and id=:id limit 1 ", array(':id' => $cartdata['optionid'], ':uniacid' => $uniacid, ':goodsid' => $goodsid));
                                                                    $cartoption = set_medias($cartoption, 'thumb');
                                                                    $cartspecs = explode('_', $cartoption['specs']);
                                                                    $goods = pdo_fetch("SELECT id,title,thumb,total,marketprice FROM " . tablename('ewei_shop_goods') . " WHERE id = :id", array(':id' => $goodsid));
                                                                    $goods = set_medias($goods, 'thumb');
                                                                    $allspecs = pdo_fetchall("select * from " . tablename('ewei_shop_goods_spec') . " where goodsid=:id order by displayorder asc", array(':id' => $goodsid));
                                                                    foreach ($allspecs as &$s) {
                                                                            $s['items'] = pdo_fetchall("select * from " . tablename('ewei_shop_goods_spec_item') . " where  `show`=1 and specid=:specid order by displayorder asc", array(":specid" => $s['id']));
                                                                    }
                                                                    unset($s);
                                                                    $options = pdo_fetchall("select id,title,thumb,marketprice,productprice,costprice, stock,weight,specs from " . tablename('ewei_shop_goods_option') . " where goodsid=:id order by id asc", array(':id' => $goodsid));
                                                                    $options = set_medias($options, 'thumb');
                                                                    $specs = array();
                                                                    if (count($options) > 0) {
                                                                            $specitemids = explode("_", $options[0]['specs']);
                                                                            foreach ($specitemids as $itemid) {
                                                                                    foreach ($allspecs as $ss) {
                                                                                            $items = $ss['items'];
                                                                                            foreach ($items as $it) {
                                                                                                    if ($it['id'] == $itemid) {
                                                                                                            $specs[] = $ss;
                                                                                                            break;
                                                                                                    }
                                                                                            }
                                                                                    }
                                                                            }
                                                                    }
                                                                    show_json(1, array('cartdata' => $cartdata, 'cartoption' => $cartoption, 'cartspecs' => $cartspecs, 'goods' => $goods, 'options' => $options, 'specs' => $specs));
                                                            } else {
                                                                    if ($op == 'setoption' && $_W['ispost']) {
                                                                            $id = intval($_GPC['id']);
                                                                            $goodsid = intval($_GPC['goodsid']);
                                                                            $optionid = intval($_GPC['optionid']);
                                                                            $option = pdo_fetch("select id,title,thumb,marketprice,productprice,costprice, stock,weight,specs from " . tablename('ewei_shop_goods_option') . " " . " where uniacid=:uniacid and goodsid=:goodsid and id=:id limit 1 ", array(':id' => $optionid, ':uniacid' => $uniacid, ':goodsid' => $goodsid));
                                                                            $option = set_medias($option, 'thumb');
                                                                            if (empty($option)) {
                                                                                    show_json(0, '规格未找到');
                                                                            }
                                                                            pdo_update('ewei_shop_member_cart', array('optionid' => $optionid), array('id' => $id, 'uniacid' => $uniacid, 'goodsid' => $goodsid));
                                                                            show_json(1, array('optionid' => $optionid, 'optiontitle' => $option['title']));
                                                                    } else {
                                                                            if ($op == 'updatenum' && $_W['ispost']) {
                                                                                    $id = intval($_GPC['id']);
                                                                                    $goodsid = intval($_GPC['goodsid']);
                                                                                    $total = intval($_GPC['total']);
                                                                                    empty($total) && ($total = 1);
                                                                                    $data = pdo_fetchall("select id,total from " . tablename('ewei_shop_member_cart') . " " . " where id=:id and uniacid=:uniacid and goodsid=:goodsid  and openid=:openid limit 1 ", array(':id' => $id, ':uniacid' => $uniacid, ':goodsid' => $goodsid, ':openid' => $openid));
                                                                                    if (empty($data)) {
                                                                                            show_json(0, '购物车数据未找到');
                                                                                    }
                                                                                    pdo_update('ewei_shop_member_cart', array('total' => $total), array('id' => $id, 'uniacid' => $uniacid, 'goodsid' => $goodsid));
                                                                                    show_json(1);
                                                                            } else {
                                                                                    if ($op == 'tofavorite' && $_W['ispost']) {
                                                                                            $ids = $_GPC['ids'];
                                                                                            if (empty($ids) || !is_array($ids)) {
                                                                                                    show_json(0, '参数错误');
                                                                                            }
                                                                                            foreach ($ids as $id) {
                                                                                                    $goodsid = pdo_fetchcolumn('select goodsid from ' . tablename('ewei_shop_member_cart') . ' where id=:id and uniacid=:uniacid and openid=:openid limit 1 ', array(':id' => $id, ':uniacid' => $uniacid, ':openid' => $openid));
                                                                                                    if (!empty($goodsid)) {
                                                                                                            $fav = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_member_favorite') . ' where goodsid=:goodsid and uniacid=:uniacid and openid=:openid and deleted=0 limit 1 ', array(':goodsid' => $goodsid, ':uniacid' => $uniacid, ':openid' => $openid));
                                                                                                            if ($fav <= 0) {
                                                                                                                    $fav = array('uniacid' => $uniacid, 'goodsid' => $goodsid, 'openid' => $openid, 'deleted' => 0, 'createtime' => time());
                                                                                                                    pdo_insert('ewei_shop_member_favorite', $fav);
                                                                                                            }
                                                                                                    }
                                                                                            }
                                                                                            $sql = "update " . tablename('ewei_shop_member_cart') . ' set deleted=1 where uniacid=:uniacid and openid=:openid and id in (' . implode(',', $ids) . ')';
                                                                                            pdo_query($sql, array(':uniacid' => $uniacid, ':openid' => $openid));
                                                                                            show_json(1);
                                                                                    } else {
                                                                                            if ($op == 'remove' && $_W['ispost']) {
                                                                                                    $ids = $_GPC['ids'];
                                                                                                    if (empty($ids) || !is_array($ids)) {
                                                                                                            show_json(0, '参数错误');
                                                                                                    }
                                                                                                    $sql = "update " . tablename('ewei_shop_member_cart') . ' set deleted=1 where uniacid=:uniacid and openid=:openid and id in (' . implode(',', $ids) . ')';
                                                                                                    pdo_query($sql, array(':uniacid' => $uniacid, ':openid' => $openid));
                                                                                                    show_json(1);
                                                                                                            }
                                                                                                        }
                                                                                                }
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        include $this->template('cart');
                                                   }else{
                                                       //全部分类
                                                      if($op=="category"){
                                                        $shopset = set_medias(m('common')->getSysset('shop'), 'catadvimg');
                                                        $commission = p('commission');
                                                        if ($commission) {
                                                                $shopid = intval($_GPC['shopid']);
                                                                $shop = set_medias($commission->getShop($openid), array('img', 'logo'));
                                                        }
                                                        include $this->template('category');  
                                                      }else{
                                                          if($op=="gift"){
                                                                    if($member['isShop']!=1){
                                                                    exit;
                                                                    }
                                                                    if($member['shoptype']==1 || $member['shoptype']==2 || $member['shoptype']==3){
                                                                        $style=pdo_fetchall('select * from '.tablename('ewei_shop_commission_gift_style').' where uniacid=:uniacid and enabled=1',array(':uniacid'=>$_W['uniacid']));
                                                                        $gift=pdo_fetch('select * from '.tablename('mc_members').'where uniacid = :uniacid and uid=:uid',array(':uniacid' => $_W['uniacid'],':uid'=>$member['uid']));
                                                                        if(!empty($_GPC['data'])){
                                                                        $data=$_GPC['data'];
                                                                        $data['mid']=$mid;
                                                                        $data['uniacid']=$_W['uniacid'];
                                                                        $data['createtime']=time();
                                                                        if($gift['gift'] >=$data['money'] && $data['money']>0){
                                                                        $money=$gift['gift']-$data['money'];
                                                                        pdo_update('mc_members',array('gift'=>$money),array('uniacid' => $_W['uniacid'],'uid'=>$member['uid']));
                                                                        pdo_insert('ewei_shop_commission_gift_log', $data);
                                                                        $log_id = pdo_insertid();
                                                                        show_json(1,array('xiaodian'=>$mid,'log_id'=>$log_id));
                                                                        }else{
                                                                        show_json(0);    
                                                                        }
                                                                        }    
                                                                       include $this->template('gift');
                                                                    }
                                                          }else{
                                                              if($op=="gift_log"){
                                                                  if($member['isShop']!=1){
                                                                    exit;
                                                                  }
                                                                  if($member['shoptype']==1 || $member['shoptype']==2 || $member['shoptype']==3){
                                                                    $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_gift_log').' where uniacid=:uniacid and mid=:mid order by id desc ',array(':uniacid'=>$_W['uniacid'],':mid'=>$mid));
                                                                    $total=pdo_fetchcolumn('select count(*) from '.tablename('ewei_shop_commission_gift_log').' where uniacid=:uniacid and mid=:mid order by id desc ',array(':uniacid'=>$_W['uniacid'],':mid'=>$mid));
                                                                    //$totalmoney=0;
                                                                    foreach($list as $key=>$value){
                                                                      if($value['openid']){
                                                                      $info=pdo_fetch('select * from '.tablename('ewei_shop_member').' where uniacid=:uniacid and openid=:openid order by id desc ',array(':uniacid'=>$_W['uniacid'],':openid'=>$value['openid']));
                                                                      $list[$key]['getname']=$info['nickname'];
                                                                      $list[$key]['gettime']=date('Y-m-d  H:m:i',$value['gettime']);
                                                                      }else{
                                                                      $list[$key]['getname']="";
                                                                      $list[$key]['gettime']="";    
                                                                      }
                                                                      $list[$key]['createtime']=date('Y-m-d H:m:i',$value['createtime']);
                                                                      //$totalmoney=$totalmoney+$value['money'];
                                                                    }
                                                                    include $this->template('gift_log'); 
                                                                  }
                                                               }else{
                                                                  if($op=="gold"){
                                                                       if($member['isShop']!=1){
                                                                        exit;
                                                                        }
                                                                        if($member['shoptype']==1 || $member['shoptype']==2 || $member['shoptype']==3){
                                                                               $gold=pdo_fetch('select * from '.tablename('mc_members').'where uniacid = :uniacid and uid=:uid',array(':uniacid' => $_W['uniacid'],':uid'=>$member['uid']));
                                                                                if(!empty($_GPC['data'])){
                                                                                    $data=$_GPC['data'];
                                                                                    $data['mid']=$mid;
                                                                                    $data['uniacid']=$_W['uniacid'];
                                                                                    $data['createtime']=time();
                                                                                    if($gold['gold'] >=$data['money'] && $data['money']>0){
                                                                                        $money=$gold['gold']-$data['money'];
                                                                                        pdo_update('mc_members',array('gold'=>$money),array('uniacid' => $_W['uniacid'],'uid'=>$member['uid']));
                                                                                        pdo_insert('ewei_shop_commission_gold_log', $data);
                                                                                        $gold_id = pdo_insertid();
                                                                                        
                                                                                        //生成领取二维码
                                                                                        require IA_ROOT . '/framework/library/qrcode/phpqrcodecolor.php';
                                                                                        $path = IA_ROOT . "/addons/ewei_shop/data/gold/" . $_W['uniacid'] . "/";
                                                                                        $time = time();
                                                                                        if (!is_dir($path)) {
                                                                                                load()->func('file');
                                                                                                mkdirs($path);
                                                                                        }
                                                                                        $jumpurl = 'http://wx.lanxuevip.com/app/index.php?i=5&c=entry&do=gold&m=ewei_shop&gold_id='.$gold_id;
                                                                                        $file = $gold_id . '.png';
                                                                                        $qr_file = $path . $file;
                                                                                        $color =0;
                                                                                        $jumpurl = $jumpurl . '&createtime=' .$time;
                                                                                        QRcode::png($jumpurl, $qr_file, 'L', 8, 4, false, $color);
                                                                                        show_json(1,array('gold_id'=>$gold_id));
                                                                                    }else{
                                                                                        show_json(0);    
                                                                                    }
                                                                                }    
                                                                               include $this->template('gold');
                                                                           }
                                                                  }else{
                                                                      if($op=="gold_log"){
                                                                           if($member['isShop']!=1){
                                                                           exit;
                                                                            }
                                                                            if($member['shoptype']==1 || $member['shoptype']==2 || $member['shoptype']==3){
                                                                                $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_gold_log').' where uniacid=:uniacid and mid=:mid order by id desc ',array(':uniacid'=>$_W['uniacid'],':mid'=>$mid));
                                                                                //$totalmoney=0;
                                                                                foreach($list as $key=>$value){
                                                                                  $list[$key]['createtime']=date('Y-m-d',$value['createtime']);
                                                                                  //$totalmoney=$totalmoney+$value['money'];
                                                                                }
                                                                                include $this->template('gold_log');
                                                                            }
                                                                      }else{
                                                                           if($op=="gold_receive"){
                                                                               if(!empty($_GPC['gold_id'])){
                                                                               $list=pdo_fetch('select * from '.tablename('ewei_shop_commission_gold_log').' where uniacid=:uniacid and id=:id order by id desc ',array(':uniacid'=>$_W['uniacid'],':id'=>$_GPC['gold_id']));    
                                                                               }else{
                                                                                  exit; 
                                                                               }
                                                                               include $this->template('gold_receive');
                                                                           }else{
                                                                               if($op=="activity_list"){
                                                                                   if($member['isShop']==1){
                                                                                   include $this->template('activity_list'); 
                                                                                   }
                                                                               }else{
                                                                                   
                                                         if($op=="experience"){
                                                              if($member['isShop']!=1){
                                                                    exit;
                                                              }
                                                                if($member['shoptype']==2 || $member['shoptype']==3){
                                                                    if(!empty($_GPC['data'])){
                                                                         $data=$_GPC['data'];
                                                                         $data['mid']=$mid;
                                                                         $data['uniacid']=$_W['uniacid'];
                                                                         $data['openid']=$openid;
                                                                         $data['createtime']=time();
                                                                         $data['starttime']=strtotime($_GPC['data']['starttime']);
                                                                         $data['endtime']=strtotime($_GPC['data']['endtime']);
                                                                         if($data['starttime'] == false && $data['endtime'] == false){
                                                                         show_json(0,'请选择时间！');    
                                                                         }
                                                                         if($data['endtime']<$data['starttime']){
                                                                         show_json(0,'开始时间不能大于结束时间！');
                                                                         }
                                                                         if($data['endtime']<time()){
                                                                         show_json(0,'结束时间一定要大于当前时间！'); 
                                                                         }
                                                                         if($data['endtime']>$data['starttime'] && $data['endtime']>time() && $data['num']>0 && $data['money']>0 && $data['cash']>0){
                                                                         pdo_insert('ewei_shop_commission_experience',$data);
                                                                         $experience_id = pdo_insertid();
                                                                         show_json(1,array('experienceid'=>$experience_id));
                                                                         }else{
                                                                         show_json(0,'操作失败！');
                                                                         }
                                                                    }
                                                                  include $this->template('experience'); 
                                                                }
                                                         }else{
                                                             if($op=="experience_log"){
                                                                 if($member['isShop']!=1){
                                                                    exit;
                                                                }
                                                                if($member['shoptype']==2 || $member['shoptype']==3){
                                                                        $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_experience').' where uniacid=:uniacid and mid=:mid order by id desc ',array(':uniacid'=>$_W['uniacid'],':mid'=>$mid));
                                                                        foreach($list as $key=>$value){
                                                                          $usetotal=  pdo_fetchcolumn('select count(*) from '.tablename('ewei_shop_commission_experience_record').' where uniacid=:uniacid and experienceid=:experienceid and status=1 and isuse=1 order by id desc ',array(':uniacid'=>$_W['uniacid'],':experienceid'=>$value['id']));
                                                                          $list[$key]['usetotal']=$usetotal;//已使用的体验券个数
                                                                          $list[$key]['nousetotal']=$value['receive_num']-$usetotal;//未使用的体验券个数
                                                                          $list[$key]['yue']=$value['num']-$value['receive_num'];
                                                                          $list[$key]['createtime']=date('Y-m-d H:i:s',$value['createtime']);
                                                                        }
                                                                     include $this->template('experience_log');   
                                                                }
                                                             }else{
                                                                 if($op=="experience_all_record"){
                                                                   if($member['isShop']!=1){
                                                                    exit;
                                                                    }
                                                                    if($member['shoptype']==2 || $member['shoptype']==3){
                                                                            $list=pdo_fetchall('select * from '.tablename('ewei_shop_commission_experience_record').' where uniacid=:uniacid and mid=:mid and status=1 order by id desc ',array(':uniacid'=>$_W['uniacid'],':mid'=>$mid));
                                                                            foreach($list as $key=>$value){
                                                                             $exper=pdo_fetch('select * from '.tablename('ewei_shop_commission_experience').' where uniacid=:uniacid and id=:id order by id desc ',array(':uniacid'=>$_W['uniacid'],':id'=>$value['experienceid']));
                                                                             $list[$key]['title']=$exper['title'];
                                                                             $list[$key]['createtime']=date('Y-m-d H:i:s',$value['createtime']);
                                                                             if($value['usetime']){
                                                                             $list[$key]['usetime']=date('Y-m-d H:i:s',$value['usetime']);    
                                                                             }else{
                                                                             $list[$key]['usetime']="";  
                                                                             }
                                                                            }
                                                                            include $this->template('experience_all_record');
                                                                    }
                                                                 }else{
                                                                 if($op=="search"){
                                                                        $keywords = trim($_GPC['keywords']);
                                                                        
                                                                        $goods = m('goods')->getList(array('pagesize' => 100000,'keywords' => trim($_GPC['keywords']),'ids'=>$commission_shop['goodsids']));
                                                                        show_json(1, array('list' => $goods)); 
                                                                 }else{    
                                                                            //全部商品
                                                                            if($op=="allgoods"){
                                                                                 $set = m('common')->getSysset('shop');
                                                                                  $commission = p('commission');
                                                                                  if ($commission) {
                                                                                          $shopid = intval($_GPC['shopid']);
                                                                                          if (!empty($shopid)) {
                                                                                                  $myshop = set_medias($commission->getShop($shopid), array('img', 'logo'));
                                                                                          }
                                                                                  }
                                                                                  $current_category = false;
                                                                                  if (!empty($_GPC['tcate'])) {
                                                                                          $current_category = pdo_fetch('select id,parentid,name,level from ' . tablename('ewei_shop_category') . ' where id=:id and uniacid=:uniacid order by displayorder DESC', array(':id' => intval($_GPC['tcate']), ':uniacid' => $_W['uniacid']));
                                                                                  } else {
                                                                                          if (!empty($_GPC['ccate'])) {
                                                                                                  $current_category = pdo_fetch('select id,parentid,name,level from ' . tablename('ewei_shop_category') . ' where id=:id and uniacid=:uniacid order by displayorder DESC', array(':id' => intval($_GPC['ccate']), ':uniacid' => $_W['uniacid']));
                                                                                          } else {
                                                                                                  if (!empty($_GPC['pcate'])) {
                                                                                                          $current_category = pdo_fetch('select id,parentid,name,level from ' . tablename('ewei_shop_category') . ' where id=:id and uniacid=:uniacid order by displayorder DESC', array(':id' => intval($_GPC['pcate']), ':uniacid' => $_W['uniacid']));
                                                                                                  }
                                                                                          }
                                                                                  }
                                                                                  $parent_category = pdo_fetch('select id,parentid,name,level from ' . tablename('ewei_shop_category') . ' where id=:id  and uniacid=:uniacid limit 1', array(':id' => $current_category['parentid'], ':uniacid' => $_W['uniacid']));
                                                                                  if ($_W['isajax']) {
                                                                                          $args = array('pagesize' => 10, 'page' => $_GPC['page'], 'isnew' => $_GPC['isnew'], 'ishot' => $_GPC['ishot'], 'isrecommand' => $_GPC['isrecommand'], 'isdiscount' => $_GPC['isdiscount'], 'istime' => $_GPC['istime'], 'keywords' => $_GPC['keywords'], 'pcate' => $_GPC['pcate'], 'ccate' => $_GPC['ccate'], 'tcate' => $_GPC['tcate'], 'order' => $_GPC['order'], 'by' => $_GPC['by']);
                                                                                          if (!empty($commission_shop['selectgoods']) && !empty($commission_shop['goodsids'])) {
                                                                                                  $args['ids'] = $commission_shop['goodsids'];
                                                                                          }
                                                                                          $goods = m('goods')->getList($args);
                                                                                          if(empty($commission_shop['goodsids'])){
                                                                                            $goods=array();   
                                                                                           }
                                                                                          $category = false;
                                                                                          if (intval($_GPC['page']) <= 1) {
                                                                                                  if (!empty($_GPC['tcate'])) {
                                                                                                          $parent_category1 = pdo_fetch('select id,parentid,name,level from ' . tablename('ewei_shop_category') . ' where id=:id  and uniacid=:uniacid limit 1', array(':id' => $parent_category['parentid'], ':uniacid' => $_W['uniacid']));
                                                                                                          $category = pdo_fetchall('select id,name,level from ' . tablename('ewei_shop_category') . ' where parentid=:parentid and enabled=1 and uniacid=:uniacid order by level asc, isrecommand desc, displayorder DESC', array(':parentid' => $parent_category['id'], ':uniacid' => $_W['uniacid']));
                                                                                                          $category = array_merge(array(array('id' => 0, 'name' => '全部分类', 'level' => 0), $parent_category1, $parent_category), $category);
                                                                                                  } else {
                                                                                                          if (!empty($_GPC['ccate'])) {
                                                                                                                  if (intval($set['catlevel']) == 3) {
                                                                                                                          $category = pdo_fetchall('select id,name,level from ' . tablename('ewei_shop_category') . ' where (parentid=:parentid or id=:parentid) and enabled=1  and uniacid=:uniacid order by level asc, isrecommand desc, displayorder DESC', array(':parentid' => intval($_GPC['ccate']), ':uniacid' => $_W['uniacid']));
                                                                                                                  } else {
                                                                                                                          $category = pdo_fetchall('select id,name,level from ' . tablename('ewei_shop_category') . ' where parentid=:parentid and enabled=1 and uniacid=:uniacid order by level asc, isrecommand desc, displayorder DESC', array(':parentid' => $parent_category['id'], ':uniacid' => $_W['uniacid']));
                                                                                                                  }
                                                                                                                  $category = array_merge(array(array('id' => 0, 'name' => '全部分类', 'level' => 0), $parent_category), $category);
                                                                                                          } else {
                                                                                                                  if (!empty($_GPC['pcate'])) {
                                                                                                                          $category = pdo_fetchall('select id,name,level from ' . tablename('ewei_shop_category') . ' where (parentid=:parentid or id=:parentid) and enabled=1 and uniacid=:uniacid order by level asc, isrecommand desc, displayorder DESC', array(':parentid' => intval($_GPC['pcate']), ':uniacid' => $_W['uniacid']));
                                                                                                                          $category = array_merge(array(array('id' => 0, 'name' => '全部分类', 'level' => 0)), $category);
                                                                                                                  } else {
                                                                                                                          $category = pdo_fetchall('select id,name,level from ' . tablename('ewei_shop_category') . ' where parentid=0 and enabled=1 and uniacid=:uniacid order by displayorder DESC', array(':uniacid' => $_W['uniacid']));
                                                                                                                  }
                                                                                                          }
                                                                                                  }
                                                                                                  foreach ($category as &$c) {
                                                                                                          if ($current_category['id'] == $c['id']) {
                                                                                                                  $c['on'] = true;
                                                                                                          }
                                                                                                  }
                                                                                                  unset($c);
                                                                                          }
                                                                                          show_json(1, array('goods' => $goods, 'pagesize' => $args['pagesize'], 'category' => $category, 'current_category' => $current_category));
                                                                                  }
                                                                                  include $this->template('list');
                                                                            }else{
                                                                              if($op=="user"){
                                                                                    if($member['isShop']==1){
                                                                                     include $this->template('user');  
                                                                                    }
                                                                              }   
                                                                            }
                                }                               
                              }
                            }
                          }            
                        }
                      }
                    }
                  }
                }         
            }         
          }
         }
       }
     }
   }
  }
}